package com.tongdainfo.common.parser;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;

/**
 * 功能描述：txt文件内容解析类
 *
 * @author zhao
 * @version 2010-7-10 1.0
 */
public class TxtParser implements IParser {

    private String[] fileTypes = {"txt"};

    public String[] getFileTypes() {
        return fileTypes;
    }

    public String readText(File file, String charset) throws Exception {
        FileChannel inChannel = null;
        ByteBuffer bb = ByteBuffer.allocate(512);
        Charset cs = Charset.forName(charset);
        StringBuffer sb = new StringBuffer();
        FileInputStream inFile = null;
        CharBuffer cb = null;
        try {
            inFile = new FileInputStream(file);
            inChannel = inFile.getChannel();
            while (inChannel.read(bb) != -1) {
                bb.flip();
                cb = cs.decode(bb);
                sb.append(cb);
                bb.clear();
            }
        } finally {
            if (inFile != null) {
                try {
                    inFile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }

    public static void main(String[] s) throws Exception {
        long total = 0L;
        for (int i = 0; i < 1; i++) {
            long start = System.currentTimeMillis();
            System.out.println(new TxtParser().readText(
                    new File("C:\\Users\\aiju\\Desktop\\1.html"), "gbk"));
//            new File("./1.txt"), "gb2312"));
            total += System.currentTimeMillis() - start;

        }
        System.out.print("耗时：" + total);
    }

	@Override
	public String readInput(InputStream input, String charset) throws Exception {
		
//		FileInputStream fileInputStream = new FileInputStream("D:\\1.txt");
        InputStreamReader inputStreamReader = new InputStreamReader(input, charset);
        
		BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
         
        StringBuffer sb = new StringBuffer();
        String text = null;
        while((text = bufferedReader.readLine()) != null){
            sb.append(text);
        }
        
//        System.err.println(sb.toString());
        return sb.toString();
		/*FileChannel inChannel = null;
        ByteBuffer bb = ByteBuffer.allocate(512);
        Charset cs = Charset.forName(charset);
        StringBuffer sb = new StringBuffer();
        FileInputStream inFile = null;
        CharBuffer cb = null;
        try {
            inFile = (FileInputStream) input;
            inChannel = inFile.getChannel();
            while (inChannel.read(bb) != -1) {
                bb.flip();
                cb = cs.decode(bb);
                sb.append(cb);
                bb.clear();
            }
        } finally {
            if (inFile != null) {
                try {
                    inFile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();*/
	}
}
